package com.example.imagedemo;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ImageView myImgview;
    SeekBar myseekbar1,myseekbar2;
    TextView myTextv1,myTextv2;
    int minWidth=200;
    Matrix matrix=new Matrix();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image);

        myImgview = (ImageView)findViewById(R.id.myimg);
        myseekbar1 = (SeekBar)findViewById(R.id.imageSize);
        myseekbar2 = (SeekBar)findViewById(R.id.imageAngle);
        myTextv1 = (TextView)findViewById(R.id.heightAndWidth);
        myTextv2 = (TextView)findViewById(R.id.angle);
        final BitmapDrawable bitmapDrawable=(BitmapDrawable) getResources().getDrawable(R.drawable.setlate);//通过路径找到mipmap中的str
        myseekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(myImgview.getLayoutParams());
                margin.setMargins((1000-progress)/2,(600-progress)/2,(1000-progress)/2,(600-progress)/2);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
                layoutParams.height=minWidth+progress;
                layoutParams.width =minWidth+progress;
                myImgview.setLayoutParams(layoutParams);
                myTextv1.setText("图像宽度：" + layoutParams.width + " 图像高度：" + layoutParams.height);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        myseekbar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Bitmap bitmap=bitmapDrawable.getBitmap();//获取图源
                matrix.setRotate(progress);//设置角度
                bitmap=bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,true);
                myImgview.setImageBitmap(bitmap);//图片显示
                myTextv2.setText("(沿中心)旋转角度："+progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}
